using System;
using System.Drawing;
using System.ComponentModel;

namespace WebChart
{
	public class ColumnChart : Chart
	{
		// Fields
		private float dataLabelOffset;
		private int maxColumnWidth;

		// Methods
		public ColumnChart()
		{
			this.maxColumnWidth = 0;
			this.dataLabelOffset = 0f;
			base.showLineMarkers = false;
		}

		public ColumnChart(ChartPointCollection data) : base(data)
		{
			this.maxColumnWidth = 0;
			this.dataLabelOffset = 0f;
			base.showLineMarkers = false;
		}

		public ColumnChart(ChartPointCollection data, Color color) : base(data, color)
		{
			this.maxColumnWidth = 0;
			this.dataLabelOffset = 0f;
			base.showLineMarkers = false;
		}

		protected override RectangleF GetDataLabelRectangle(Graphics graphics, float value, string text, int maxWidth, int pointIndex)
		{
			RectangleF ef = base.GetDataLabelRectangle(graphics, value, text, maxWidth, pointIndex);
			ef.Offset(this.dataLabelOffset, 0f);
			return ef;
		}

		public override void Render(Graphics graphics, int width, int height)
		{
			float maxColumnWidth;
			ChartEngine engine = base.Engine;
			float scaleX = engine.scaleX;
			float scaleY = engine.scaleY;
			Pen pen = base.Line.GetPen(graphics);
			Brush brush = base.Fill.GetBrush(graphics);
			bool flag = (base.shadow != null) && base.Shadow.Visible;
			Brush brush2 = null;
			if (flag)
			{
				brush2 = new SolidBrush(base.Shadow.Color);
			}
			float num3 = 0f;
			int num5 = 0;
			int num6 = 0;
			if (engine.Charts.Count > 1)
			{
				foreach (Chart chart in engine.Charts)
				{
					if (chart.Equals(this))
					{
						num6 = num5;
					}
					if (chart is ColumnChart)
					{
						num5++;
					}
				}
				maxColumnWidth = (scaleX - 1f) / ((float) num5);
				num3 = (maxColumnWidth * num6) + 1f;
			}
			else
			{
				num3 = 1f;
				maxColumnWidth = scaleX;
			}
			if ((this.maxColumnWidth > 0) && (this.maxColumnWidth < maxColumnWidth))
			{
				int num7 = ((int) maxColumnWidth) - this.maxColumnWidth;
				if (num5 > 0)
				{
					num7 = (num7 / num5) / 2;
				}
				else
				{
					num7 /= 2;
				}
				maxColumnWidth = this.maxColumnWidth;
				num3 += 1 + num7;
			}
			float num10 = 0f;
			foreach (ChartPoint point in base.Data)
			{
				float y;
				float num9;
				if ((point.yvalue * scaleY) < 0f)
				{
					y = point.yvalue * scaleY;
					num9 = -(point.yvalue * scaleY);
				}
				else
				{
					y = 0f;
					num9 = point.yvalue * scaleY;
				}
				int num11 = (int) (num10 * scaleX);
				if (flag)
				{
					graphics.FillRectangle(brush2, (num11 + num3) + base.Shadow.OffsetX, y + base.Shadow.OffsetY, maxColumnWidth, num9);
				}
				graphics.FillRectangle(brush, num11 + num3, y, maxColumnWidth, num9);
				graphics.DrawRectangle(pen, num11 + num3, y, maxColumnWidth, num9);
				if (base.showLineMarkers)
				{
					base.lineMarker.Render(graphics, (int) ((num11 + num3) + (maxColumnWidth / 2f)), (y < 0f) ? ((int) y) : ((int) num9));
				}
				num10 += 1f;
			}
			if (flag)
			{
				brush2.Dispose();
			}
			if (base.dataLabels.visible)
			{
				this.dataLabelOffset = num3 - ((base.Engine.scaleX - maxColumnWidth) / 2f);
				base.RenderDataLabels(graphics);
			}
			brush.Dispose();
			pen.Dispose();
		}

		// Properties
		[DefaultValue(0)]
		public int MaxColumnWidth
		{
			get
			{
				return this.maxColumnWidth;
			}
			set
			{
				this.maxColumnWidth = value;
			}
		}
	}
}
